package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.CadresMapper;
import com.village.zpark.pojo.Cadres;
import com.village.zpark.service.CadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class CadresServiceImpl implements CadresService {
    @Autowired
    private CadresMapper cadresMapper;
    @Override
    public List<Cadres> findAll() {
        return cadresMapper.selectAll();
    }

    @Override
    public Cadres findById(Integer id) {
        return cadresMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addCadres(Cadres cadres) {
        return cadresMapper.insert(cadres);
    }

    @Override
    public int updateCadres(Cadres cadres) {
        return cadresMapper.updateByPrimaryKey(cadres);
    }

    @Override
    public int deleteCadresById(Integer id) {
        return cadresMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Cadres> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Cadres.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("name", "%" + param + "%")
                    .orLike("address", "%" + param + "%")
                    .orLike("education", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Cadres> cadres;
        if (param != null && !param.isEmpty()) {
            cadres = cadresMapper.selectByExample(example);
        } else {
            cadres = cadresMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(cadres);
    }
}
